<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/21
 * Time: 上午11:44
 */

namespace App\Http\Services;

use App\Http\Model\Medal;
use App\Http\Model\Room;
use App\Http\Model\RoomHostMicro;
use App\Http\Model\RoomMicro;
use App\Http\Model\RoomWaitbit;
use App\Http\Model\User;
use App\Http\Model\Hint;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\RoomUserProtect;

class RoomWaitbitServices extends CommonServices
{
    protected $model_room;
    protected $model_user;
    protected $model_room_waitbit;
    protected $model_room_micro;
    protected $model_room_hostmicro;
    protected $micro_lock_key = "micro";
    protected $model_userwallet;
    protected $modelMedal;
    protected $micUserProtectModel;

    public function __construct()
    {
        $this->model_room = new Room();
        $this->model_user = new User();
        $this->model_room_waitbit = new RoomWaitbit();
        $this->model_room_micro = new RoomMicro();
        $this->model_room_hostmicro = new RoomHostMicro();
        $this->model_userwallet = new FinanceUserWallet();
        $this->modelMedal = new Medal();
        $this->micUserProtectModel = new RoomUserProtect();
    }

    /**
     * 获取待麦列表
     * @param $room_id
     * @return array|int|mixed
     */
    public function get_users($room_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_ids = $this->model_room_waitbit->get_usersids($room_id);
        if(empty($user_ids)){
            return [];
        }
        $user_id_list = [];
        $user_id_map = [];
        $user_map = [];
        $wallet_map = [];


        for ($i = 0; $i < count($user_ids); $i++){
            array_push($user_id_list, $user_ids[$i]["user_id"]);
            $user_id_map[$user_ids[$i]["user_id"]] = $user_ids[$i]["guard_type"];
        }
        $users = $this->model_user->get_users_by_ids($user_id_list);
        for ($i = 0; $i < count($users); $i++){
            $user_map[$users[$i]["user_id"]] = $users[$i];
            //$users[$i]["guard_type"] = intval($user_id_map[$users[$i]["user_id"]]);
        }
        $wallets = $this->model_userwallet->get_wallets($user_id_list);
        for ($i = 0; $i < count($wallets); $i++){
            $wallet_map[$wallets[$i]['user_id']] = $wallets[$i];
        }
        $data = [];
        $medalArr = $this->modelMedal->getMedalUserListCache($user_id_list, false);
        $medalData = [];
        foreach ($medalArr as $val){
            if(empty($val)){
                continue;
            }
            $medalData[$val[0]['user_id']] = $val;
        }
        for ($i = 0; $i < count($user_id_list); $i++){
            $user_map[$user_id_list[$i]]['guard_type'] = intval($user_id_map[$user_id_list[$i]]);
            $user_map[$user_id_list[$i]]['diamond_balance']  = $wallet_map[$user_id_list[$i]]['diamond_balance'];
            $user_map[$user_id_list[$i]]['charm_balance']  = $wallet_map[$user_id_list[$i]]['charm_balance'];
            $user_map[$user_id_list[$i]]['wealth_balance']  = $wallet_map[$user_id_list[$i]]['wealth_balance'];
            $user_map[$user_id_list[$i]]['reputation_balance']  = $wallet_map[$user_id_list[$i]]['reputation_balance'];
            $user_map[$user_id_list[$i]]['celebrity_balance']  = $wallet_map[$user_id_list[$i]]['celebrity_balance'];
            if (array_key_exists($user_id_list[$i], $medalData)){
                $user_map[$user_id_list[$i]]['medal'] = $medalData[$user_id_list[$i]];
            }
            else{
                $user_map[$user_id_list[$i]]['medal'] = [];
            }
            array_push($data, $user_map[$user_id_list[$i]]);
        }
        return $data;
    }


    /**
     * 上麦申请
     * @param $room_id
     * @param $type  0.没有守护 1.青铜 2.白银 3.黄金
     * @return int
     */
    public function apply($room_id, $type, &$user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];

        if ($this->model_room_waitbit->get_guard_type($room_id, $user_id) !== null){
            return Hint::ROOM_WAITBIT_IS_EXIST;
        }
        if ($this->model_room_micro->get_user_micro_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        if ($this->model_room_hostmicro->get_user_status($room_id, $user_id) !== null){
            return Hint::ROOM_MICRO_USER_IS_EXIST;
        }
        $this->model_room_waitbit->add_user($room_id, $user_id, $type);
        return true;
    }



    protected function allowMicros($room_id, $guard_type = 0){
        return $this->model_room_micro->get_micros($room_id, 0);
    }

    /**
     * 允许上麦 (需要加锁完成上麦下麦动作)
     * @param $room_id
     * @param $user_id
     * @return int|bool
     */
    public function accept($room_id, $user_id, &$micro_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $current_user_id = $current_user['user_id'];
        $role = $this->model_room->get_role($room_id, $current_user_id);
        if ($role == 0){
            return Hint::AUTH_ERROR;
        }
        $guard_type = $this->model_room_waitbit->get_guard_type($room_id, $user_id);
        if ($guard_type === null){
            return Hint::ROOM_WAITBIT_NOT_EXIST;
        }

        //获取房间类型
        $roomInfo = $this->model_room->get_room($room_id);

        $micros = $this->allowMicros($room_id, $guard_type);
        $micro = null;
        for ($i = 0; $i < count($micros); $i++){
            if ($micros[$i]['user_id'] == null && !$micros[$i]['lock']){
                $micro = $micros[$i];
                if (!redis_lock($this->micro_lock_key.$room_id, 5)){
                    return Hint::NOT_ALLOWED;
                }
                $this->model_room_waitbit->del_user($room_id, $user_id);
                $this->model_room_micro->set_into_begin($room_id, $user_id, $micro["microphone_id"]);
                break;
            }
        }
        if ($micro == null){
            return Hint::ROOM_MICRO_IS_FULL;
        }
        $micro_id = $micro["microphone_id"];
        return $micro;
    }


    /**
     * 拒绝上麦
     * @param $room_id
     * @param $user_id
     * @return int
     */
    public function reject($room_id, $user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $current_user_id = $current_user['user_id'];
        if($this->model_room->get_role($room_id, $current_user_id) == 0){
            return Hint::AUTH_ERROR;
        }
        $this->model_room_waitbit->del_user($room_id, $user_id);
        return true;
    }

    /**
     * 取消上麦
     * @param $room_id
     * @return int
     */
    public function cancel($room_id, &$user_id){
        $current_user = $this->model_user->get_current_user();
        if (empty($current_user)){
            return Hint::LOGIN_OUT;
        }
        $user_id = $current_user['user_id'];
        $this->model_room_waitbit->del_user($room_id, $user_id);
        return true;
    }


    /**
     * 清空待麦
     * @param $room_id
     * @return bool|int
     */
    public function clean($room_id, $auth = true){
        if ($auth){
            $current_user = $this->model_user->get_current_user();
            if (empty($current_user)){
                return Hint::LOGIN_OUT;
            }
            $current_user_id = $current_user['user_id'];
            if($this->model_room->get_role($room_id, $current_user_id) == 0){
                return Hint::AUTH_ERROR;
            }
        }
        $this->model_room_waitbit->del_users($room_id);
        return true;
    }


    /**
     * 语音平台回调删除待麦用户
     * @param $room_id
     * @param $user_id
     */
    public function callback_del($room_id, $user_id){
        $this->model_room_waitbit->del_user($room_id, $user_id);
    }


}